const Main = require('../models/main');
const _Comment = require('../models/comment');
const formidable = require('formidable');
const fs = require('fs');
//首页推荐
exports.HomeInit = function (req, res) {
    const address = req.body.Position;
    const role = req.body.role;
    Main.find({ 'address': address, 'role': role }, null, { limit: 5, sort: { 'like': -1 } },
			function (err, result) {
				if (err) {
						return console.log(err)
				}
				return res.send(result)
			})
};

//总排行
exports.Rank = function (req, res) {
    const address = req.body.Position;
    Main.find({ 'address': address }, null, { limit: 20, sort: { 'like': -1 } },
        function (err, result) {
            if (err) {
                return console.log(err)
            }
            return res.send(result)
        })
};
//发现页
exports.Find = function (req, res) {
    var role = req.body.role;
    Main.find({ 'role': role }, function (err, result) {
        if (err) {
            return console.log(err)
        }
        return res.send(result)
    })
};

//删除
exports.Delete = function (req, res) {
    var delJson = req.body.delJson;
    for (key in delJson) {
        Main.remove({ key: delJson[key] }, function (err, result) {
            if (err) {
                return console.log(err);
            } if (result) {
                return res.send('删除成功')
            } else {
                return res.send('删除失败')
            }
        })
    }
};
//发布
exports.Fabu = function (req, res) {
    var _main = req.body.main;
    var main = new Main(_main);

    main.save(function (err, result) {
        if (err) {
            return console.log(err);
        } if (result) {
            return res.send('发布成功')
        } else {
            return res.send('发布失败')
        }
    })
};
//我的发布
exports.myFabu = function (req, res) {
    var publish_man = req.body.publish_man
    Main.find({ 'publish_man': publish_man }, function (err, result) {
        if (err) {
            return console.log(err);
        } if (result) {
            return res.send(result)
        } else {
            return res.send('获取失败')
        }
    })
};
//删除我的发布
exports.DelmyFabu = function (req, res) {
    var id = req.body.id
    Main.remove({ 'id': id }, function (err, result) {
        if (err) {
            return console.log(err);
        } if (result) {
            return res.send(result)
        } else {
            return res.send('获取失败')
        }
    })
};
//详情
exports.Msg = function (req, res) {
    var id = req.body.id
    Main.find({ '_id': id }, function (err, result) {
        if (err) {
            return console.log(err);
        } if (result) {
            return res.send(result)
        } else {
            return res.send('获取失败')
        }
    })
}
//点赞
exports.Zan = function (req, res) {
    var _id = req.body.id;
    var _like = req.body.Nlike;
    Main.update({ _id: _id }, { "$set": { like: _like } }, function (err, result) {
        if (err) {
            return console.log(err);
        } if (result) {
            return res.send(result);
        }   
    })
}
//评论 
exports.Com = function (req, res) {
    var goods_ID = req.body.id
    _Comment.find({ goods_id: goods_ID }, function (err, comments) {
        if (err) {
            return console.log(err);
        }
        if (comments) {
            console.log(comments);
            return res.send(comments);
        }
    })
}
//搜索
exports.Search = function(req,res){
    var title = req.body.sch;
    Main.find({title:{$regex:title,$options:"i"}},function(err,result){
        if(err){
            return console.log(err);
        }if(result){
            return res.send(result);
        }
    })
}
//上传图片
exports.uploadpic = function (req, res) {
    let formParse = new formidable.IncomingForm();
    let uploadUrl = './public/images/pic';
    formParse.uploadDir = uploadUrl; //缓存地址  
    formParse.keepExtensions = true; //是否包含文件后缀  
    let files = [];
    // 文件都将保存在files数组中  
    formParse.on('file', function (field, file) {
        files.push([field, file]);
    })
    formParse.parse(req, function (error, fields, files) {
        console.log(files.file.path);
        let picurl = 'http://localhost:3000/' + files.file.path.slice(7);
        console.log(picurl)
        res.send(picurl)
    })
}